﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.BaseConfigs;
using HamcoWcs.Models.Common.Enums;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HamcoWcs.Service.Common
{
    [AutoInject(typeof(IBaseConfigService), InjectType.Single)]
    public class BaseConfigService : IBaseConfigService
    {
        private WcsDbContext _wcsDbContext => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<WcsDbContext>();
        private readonly IMapper _mapper;
        private readonly IServiceScopeFactory _scopeFactory;
        private Dictionary<string, string> _configDic = new Dictionary<string, string>();
        public BaseConfigService(IMapper mapper, IServiceScopeFactory serviceScopeFactory)
        {
            //_configDic = new Dictionary<string, string>();
            _mapper = mapper;
            _scopeFactory = serviceScopeFactory;
            using (var context = _wcsDbContext)
            {
                _configDic = context.BaseConfigs.ToDictionary(k => k.ConfigKey, v => v.ConfigValue);
            }


            //GlobalValue
        }
        public string GetConfigValue(string configKey)
        {
            if (!_configDic.ContainsKey(configKey)) throw new Exception($"未找到对应的Key{configKey}]");
            return _configDic[configKey];
        }

        public bool TryGetConfigValue(string key, out string value)
        {
            return _configDic.TryGetValue(key, out value);
        }

        public bool SetConfigMsg(string configKey, string configValue)
        {
            if (_configDic.ContainsKey(configKey))
            {
                using (var context = _wcsDbContext)
                {
                    var config = context.BaseConfigs.FirstOrDefault(x => x.ConfigKey == configKey);
                    config.ConfigValue = configValue;
                    context.BaseConfigs.Update(config);
                    if (context.SaveChanges() > 0)
                    {
                        _configDic[configKey] = configValue;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return false;
        }

        public void AddConfig(BaseConfig config)
        {

            if (_configDic.ContainsKey(config.ConfigKey))
                throw new Exception($"已存在对应的Key{config.ConfigKey}]");

            try
            {
                using (var context = _wcsDbContext)
                {
                    context.BaseConfigs.Add(config);
                    var result = context.SaveChanges() > 0;
                    if (result) _configDic.Add(config.ConfigKey, config.ConfigValue);
                }
            }
            catch (Exception)
            {
                throw new Exception("DB保存失败");
            }
        }

        public bool DeleteConfig(string key)
        {
            if (!_configDic.ContainsKey(key)) throw new Exception($"未找到对应的[{key}]");

            using (var context = _wcsDbContext)
            {
                var config = context.BaseConfigs.FirstOrDefault(d => d.ConfigKey == key);
                if (config == null) throw new Exception($"未找到[{key}]");
                context.Remove(config);
                try
                {
                    var result = context.SaveChanges() > 0;
                    if (result) _configDic.Remove(key);
                    return result;
                }
                catch (Exception)
                {
                    throw new Exception("DB保存失败");
                }
            }
        }
    }
}
